﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.hangqing
{
    public partial class xunjia_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.xunjia().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.xunjia bll = new BLL.xunjia();
            Model.xunjia model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            txtUserID.Text = model.user_id.ToString();
            txtRemark.Text = model.remark;
            if (model.user_id > 0)
            {
                lbUserName.Text = new BLL.users().GetUserName(model.user_id);
            }
            //绑定图片相册
            rptAlbumList.DataSource = model.albums;
            rptAlbumList.DataBind();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.xunjia model = new Model.xunjia();
            BLL.xunjia bll = new BLL.xunjia();

            model.user_id = Utils.StrToInt(txtUserID.Text.Trim(), 0);
            model.remark = txtRemark.Text.Trim();
            model.add_time = DateTime.Now;
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 0);
            model.hits = 0;

            #region 保存相册====================
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            if (albumArr != null && albumArr.Length > 0)
            {
                List<Model.xunjia_albums> ls = new List<Model.xunjia_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    if (imgArr.Length == 3)
                    {

                        ls.Add(new Model.xunjia_albums { original_path = imgArr[1], thumb_path = imgArr[2] });
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.xunjia bll = new BLL.xunjia();
            Model.xunjia model = bll.GetModel(_id);

            model.user_id = Utils.StrToInt(txtUserID.Text.Trim(), 0);
            model.remark = txtRemark.Text.Trim();
            model.add_time = DateTime.Now;
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 0);

            #region 保存相册====================
            
            if (model.albums != null)
            {
                model.albums.Clear();
            }
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            if (albumArr != null)
            {
                List<Model.xunjia_albums> ls = new List<Model.xunjia_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    int img_id = Utils.StrToInt(imgArr[0], 0);
                    if (imgArr.Length == 3)
                    {
                        ls.Add(new Model.xunjia_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] });
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Update(model))
            {
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改询价信息成功！", "xunjia_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加询价信息成功！", "xunjia_list.aspx");
            }
        }

    }
}